import RouterService from './router.service'

class Container {
  services = new Map<GServiceNameType, GServiceType>()

  register(name: GServiceNameType, service: GServiceType) {
    this.services.set(name, service)
  }

  resolve(name: GServiceNameType) {
    const service = this.services.get(name)
    if (!service) throw new Error(`Service ${name} not found`)
    return service
  }

  resolves(...names: GServiceNameType[]) {
    return names.map((name) => this.resolve(name))
  }
}

let container: Container | undefined
export const s = (name: GServiceNameType) => {
  // 注册依赖
  if (container) {
    return container.resolve(name)
  }
  container = new Container()
  container.register('RouterService', new RouterService())
  return container.resolve(name)
}
